<?php
/**
 * Created by PhpStorm.
 * User: xpz
 * Date: 2021/6/10
 * Time: 10:55
 */

namespace app\api\command;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Exception;


use app\common\model\Article;

class SpiderTask extends Command
{
    private $poetryModel;
    private $poetryConentModel;
    private $poetModel;
    private $labelModel;
    private $poetryLabelModel;
    protected function configure()
    {
        $this->setName('spidertask') //定义命令的名字
        ->addArgument('method');
    }


    //php think backtask reset_score
    protected function execute(Input $input, Output $output)
    {
        require EXTEND_PATH.'phpQuery/phpQuery.php';
        $method = $input->getArgument('method');

        if ($method == 'sc_article') {
//            $this->articleNew('https://so.gushiwen.org/shiwenv_ccee5691ba93.aspx','一年级');
//            $this->article('https://app.gushiwen.cn/api/shiwen/shiwenv11.aspx?id=18284C9EFDAED75C678F1AC1A0BF7ADB&t=1568883126&token=gswapi');
//            echo "2222";
        } else if ($method == 'sc_list') {
//            $this->list();
            $this->list_old();
        } else if ($method == 'temp'){
            $this->mmList();
//            $this->mnlist();
//            $this->mnlist();
//            $this->poetry();
//            $this->poet();
        } else {
            echo "参数错误！参数列表：reset_score|del\n";
        }
    }

    //少女写真
    private function mmList(){
        //气质
        $url = 'https://www.enterdesk.com/tag-%E6%B0%94%E8%B4%A8/3.html';

//        $url = 'https://mm.enterdesk.com/pic/63316-343663.html';
        $html = $this->getCurl($url);
        \phpQuery::newDocument($html);
        $aArr = pq('dl.egeli_pic_dl dd a');
        foreach($aArr as $k=>$v){
            $tempUrl = pq($v)->attr('href');
//            echo $tempUrl."\r\n";
            $this->mmArticle($tempUrl);
        }
    }
    private function mmArticle($url){
        $domain = 'https://mm.enterdesk.com';
        $returnArr = [];
//        $url = 'https://mm.enterdesk.com/bizhi/63038-341936.html';
//        \phpQuery::newDocumentFile($url);
        $html = $this->getCurl($url);
        \phpQuery::newDocument($html);

        $image = pq('img.arc_main_pic_img')->attr('src');
        $title = pq('img.arc_main_pic_img')->attr('alt');
//        echo $title;exit;
        $returnArr[] = $image;
        $otherImage = pq('div.swiper-slide a');
        if(!empty($otherImage)){
            foreach($otherImage as $k=>$v){
                $otherUrl = pq($v)->attr('href');
                 $otherUrl."\r\n";
                $returnArr[] = $this->getMmArticle($domain.$otherUrl);
            }
        }
        if(!empty($returnArr)){
            $jsonImage = \GuzzleHttp\json_encode($returnArr);
            $image = $returnArr[0];

            $saveArr = [
                'title' => $title,
                'image' => $image,
                'images' => $jsonImage,
                'num' => count($returnArr),
                'source_url' => $url,
                'category_name' =>'气质',
                'source' => 3,
            ];
//            print_r($saveArr);
            $articleModel = new Article();
            $articleModel->save($saveArr);
        }
    }

    private function getMmArticle($url){
//        $rs = \phpQuery::newDocumentFile($url);
        $html = $this->getCurl($url);
        \phpQuery::newDocument($html);
        $image = pq('img.arc_main_pic_img')->attr('src');
        return $image;
    }


    private function mnlist(){
        $url = 'http://www.hongfen.org/darenguan/jiepaidaren/index_3.html';
        $rs = \phpQuery::newDocumentFile($url);
        $content = pq("dl dd h1 a");
        if(!empty($content)){
            foreach($content as $k=>$v){
                $url = pq($v)->attr('href');
                $this->mnarticle($url);
            }
        } else {
            echo "内容不存在！";
        }
    }

    private function mnarticle($url){
//        $url = "http://www.hongfen.org/darenguan/592160.html";
        \phpQuery::newDocumentFile($url);
//        \phpQuery::$defaultCharset='gb2312';


        $content = pq("div.article p");
        $imageArr = [];
        if(!empty($content)){
            foreach($content as $k=>$v){
                $imgUrl = pq($v)->find('img')->attr('src');
                if(!empty($imgUrl)){
                    $imageArr[] = $imgUrl;
                }
            }
        }
        if(!empty($imageArr)){
            $jsonImage = \GuzzleHttp\json_encode($imageArr);
            $image = $imageArr[0];
//            echo $image;
//            print_r($imageArr);
            $title = pq("title")->text();
//            $title = rtrim('',$title);

            $title = str_replace('_红粉女性网',"",$title);
            $saveArr = [
              'title' => $title,
              'image' => $image,
                'images' => $jsonImage,
                'num' => count($imageArr),
            'source_url' => $url,
                'category_name' =>'美女',
                'source' => 1,
            ];
            $articleModel = new Article();
            $articleModel->save($saveArr);
        } else {
            echo $url."图片为空！";
        }

//        print_r($jsonImage);
//        echo $title;
    }

    //www.umei.net  //美女写真
    private function umeiList(){
        $domain = "https://www.umei.net";
        $url = "https://www.umei.net/meinvtupian/index_5.htm";
        \phpQuery::newDocumentFile($url);
        $list = pq('div.TypeList ul li a');
        foreach($list as $k=>$v){
            $path = pq($v)->attr('href');
//            echo $domain.$path;exit;
            $this->umeiArticle($domain.$path);
        }
    }
    private function umeiArticle($url){
        $domain = "https://www.umei.net";
//        $url = "https://www.umei.net/meinvtupian/meinvxiezhen/235033.htm";
        $title = "";
        $returnArr = [];
        $aid = $this->getAidByUrl($url);
        $urlArr = parse_url($url);
        $path = str_replace($aid.".htm","",$urlArr['path']);
//        echo $path;exit;

        \phpQuery::newDocumentFile($url);
        $title = pq("title")->text();
        $title = str_replace(' - 优美图库',"",$title);
        $returnArr[] = pq('div.ImageBody p a img')->attr('src');
        $allNum = pq('div.NewPages a');
        if(!empty($allNum)){
            foreach($allNum as $k=>$v){
                $aurl = pq($v)->attr('href');
            }
        }
//        echo $aurl;
        $aurlArr = explode('_',$aurl);
//        print_r($aurlArr);exit;
        $aurlArr2 = explode('.',$aurlArr[1]);
        $allPage = (int)$aurlArr2[0];
//        print_r($aurlArr2);exit;
        if(!empty($allPage)){
            for($i=2;$i<=$allPage;$i++){
                $urltemp = $domain.$path.$aid."_".$i.".htm";
                $allUrlImg = $this->getImage($urltemp);
                if(!empty($allUrlImg)){
                    $returnArr[] = $allUrlImg;
                }
            }
        }
        if(!empty($returnArr)){
            $saveArr = [
                'title'=>$title,
              'image' => $returnArr[0],
              'images' => json_encode($returnArr),
                'num' => count($returnArr),
                'source_url' => $url,
                'category_name' =>'美女',
                'source' => 2,
            ];
//            print_r($saveArr);
            $articleModel = new Article();
            $articleModel->save($saveArr);
        }
//        print_r($returnArr);
    }

    private function getImage($url){
        \phpQuery::newDocumentFile($url);
        return pq('div.ImageBody p a img')->attr('src');
    }

    private function getAidByUrl($url)
    {
        $pattern = '/.*\/(\d*)(\.htm.*|_[\d|all]*\.htm.*)$/';
        preg_match($pattern,$url,$match);

        if (isset($match[1])) {
            return $match[1];
        }

        return false;
    }

    private function getCurl($url,$userAgent="Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; InfoPath.2; AskTbPTV/5.17.0.25589; Alexa Toolbar") {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        $result = curl_exec($ch);
        if (!$result) {
            //输出错误信息
            //trigger_error(curl_error($ch));
            return false;
        }
        curl_close($ch);
        return $result;
    }
}